package service

import (
	"errors"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"gorm.io/gorm"

	"go-admin/app/hunter/models"
	"go-admin/app/hunter/service/dto"
	"go-admin/common/actions"
	cDto "go-admin/common/dto"
)

type HtProject struct {
	service.Service
}

// GetPage 获取HtProject列表
func (e *HtProject) GetPage(c *dto.HtProjectGetPageReq, p *actions.DataPermission, list *[]models.HtProject, count *int64) error {
	var err error
	var data models.HtProject

	err = e.Orm.Model(&data).
		Scopes(
			cDto.MakeCondition(c.GetNeedSearch()),
			cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
			actions.Permission(data.TableName(), p),
		).
		Find(list).Limit(-1).Offset(-1).
		Count(count).Error
	if err != nil {
		e.Log.Errorf("HtProjectService GetPage error:%s \r\n", err)
		return err
	}
	return nil
}

// Get 获取HtProject对象
func (e *HtProject) Get(d *dto.HtProjectGetReq, p *actions.DataPermission, model *models.HtProject) error {
	var data models.HtProject

	err := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).
		First(model, d.GetId()).Error
	if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
		err = errors.New("查看对象不存在或无权查看")
		e.Log.Errorf("Service GetHtProject error:%s \r\n", err)
		return err
	}
	if err != nil {
		e.Log.Errorf("db error:%s", err)
		return err
	}
	return nil
}

// Insert 创建HtProject对象
func (e *HtProject) Insert(c *dto.HtProjectInsertReq) error {
	var err error
	var data models.HtProject
	c.Generate(&data)
	err = e.Orm.Create(&data).Error
	if err != nil {
		e.Log.Errorf("HtProjectService Insert error:%s \r\n", err)
		return err
	}
	return nil
}

// Update 修改HtProject对象
func (e *HtProject) Update(c *dto.HtProjectUpdateReq, p *actions.DataPermission) error {
	var err error
	var data = models.HtProject{}
	e.Orm.Scopes(
		actions.Permission(data.TableName(), p),
	).First(&data, c.GetId())
	c.Generate(&data)

	db := e.Orm.Save(&data)
	if err = db.Error; err != nil {
		e.Log.Errorf("HtProjectService Save error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权更新该数据")
	}
	return nil
}

// Remove 删除HtProject
func (e *HtProject) Remove(d *dto.HtProjectDeleteReq, p *actions.DataPermission) error {
	var data models.HtProject

	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Delete(&data, d.GetId())
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveHtProject error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// RemoveTalent 删除HtProject
func (e *HtProject) RemoveTalent(d *dto.HtProjectTalentDeleteReq, p *actions.DataPermission) error {
	var data models.HtProjectTalent
	db := e.Orm.Model(&data).
		Scopes(
			actions.Permission(data.TableName(), p),
		).Unscoped().Where("talent_id in ?", d.Ids).Where("project_id = ?", d.ProjectId).Delete(&data)
	if err := db.Error; err != nil {
		e.Log.Errorf("Service RemoveHtProjectTalent error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权删除该数据")
	}
	return nil
}

// BindTalent 删除HtProject
func (e *HtProject) BindTalent(d *dto.HtProjectTalentBindReq, p *actions.DataPermission) error {
	var talents []models.HtProjectTalent
	for _, id := range d.Ids {
		talents = append(talents, models.HtProjectTalent{ProjectId: d.ProjectId, TalentId: id})
	}

	db := e.Orm.Model(&talents[0]).
		Scopes(
			actions.Permission(talents[0].TableName(), p),
		).Create(&talents)
	if err := db.Error; err != nil {
		e.Log.Errorf("Service BindTalent error:%s \r\n", err)
		return err
	}
	if db.RowsAffected == 0 {
		return errors.New("无权绑定该数据")
	}
	return nil
}

// GetProjectTree 获取HtProject列表
func (e *HtProject) GetProjectTree(c *dto.HtProjectGetPageReq, p *actions.DataPermission) (m []dto.ProjectLabel, err error) {
	var data models.HtProject
	var list []models.HtProject
	err = e.Orm.Model(&data).
		Scopes(
			//cDto.MakeCondition(c.GetNeedSearch()),
			actions.Permission(data.TableName(), p),
		).
		Find(&list).Error
	if err != nil {
		e.Log.Errorf("HtProjectService GetPage error:%s \r\n", err)
		return
	}
	m = make([]dto.ProjectLabel, 0)
	for _, project := range list {
		m = append(m, dto.ProjectLabel{Id: project.Id, Label: project.Name})
	}
	return
}
